from django.db import models
from accounts.models import User
from orders.models import Order

class Payment(models.Model):
    PAYMENT_METHOD_CHOICES = (
        ('wechat', '微信支付'),
        ('alipay', '支付宝'),
        ('balance', '余额支付'),
    )
    
    PAYMENT_STATUS_CHOICES = (
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('failed', '支付失败'),
        ('refunded', '已退款'),
    )
    
    order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='payment')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='payments')
    payment_method = models.CharField(max_length=20, choices=PAYMENT_METHOD_CHOICES, verbose_name='支付方式')
    payment_status = models.CharField(max_length=20, choices=PAYMENT_STATUS_CHOICES, default='pending', verbose_name='支付状态')
    
    # 支付金额信息
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='支付金额')
    transaction_id = models.CharField(max_length=128, blank=True, verbose_name='交易号')
    
    # 微信支付特有字段
    wechat_transaction_id = models.CharField(max_length=128, blank=True, verbose_name='微信交易号')
    wechat_prepay_id = models.CharField(max_length=128, blank=True, verbose_name='预支付ID')
    
    # 时间信息
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    paid_at = models.DateTimeField(null=True, blank=True, verbose_name='支付时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'payment'
        verbose_name = '支付记录'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return f"支付#{self.id} - 订单#{self.order.id}"